<?php
/* 
 * Tools to interact with the filesystem
 * 
*/

/**
 * Description of Filesystem
 *
 * @author Yves Peeters
 */
class Filesystem {
    const GIG = 1073741824;
    const MB = 1048576;
    const KB = 1024;

    /**
     *  enter an absolute path and foldersize will return the foldersize in bytes
     *  Can be used with the showsize function to return the size in kb,mb,gb
     * @param String $path
     * @return String return
     */
    static function foldersize($path) {
        if(!file_exists($path)) return 0;
        if(is_file($path)) return filesize($path);
        $ret = 0;
        foreach(glob($path."/*") as $fn) {
            $ret += Filesystem::foldersize($fn);
        }
        return $ret;
    }

    /**
     * returns an array with all the files in a folder
     * @param String $path
     * @return Array return
     */
    static function filesInFolder($path) {
            $files = scandir($path);
            $files = array_slice($files,2);
        return $files;
    }

    /**
     * will return all the files modified after a given date
     * 
     * @param String $address
     * @param String $comparedate
     * @return Array
     * @todo return should be an array, not echo
     */
    static function filesModifiedSinceDate($address="/",$comparedate="2006-08-12 00:00:00") {

        $comparedate=strtotime($comparedate);
        @$dir = opendir($address);

        if(!$dir) {
            return 0;
        }
        while($entry = readdir($dir)) {
            if(is_dir("$address/$entry") && ($entry != ".." && $entry != ".")) {
                directory_tree("$address/$entry",$comparedate);
            }
            else {

                if($entry != ".." && $entry != ".") {

                    $fulldir=$address.'/'.$entry;
                    $last_modified = filemtime($fulldir);
                    $last_modified_str= date("Y-m-d h:i:s", $last_modified);

                    if($comparedate < $last_modified) {
                        echo $fulldir.'=>'.$last_modified_str;
                        echo "<BR>";
                    }

                }

            }

        }

    }

    /**
     * transforms a size in bytes to a more readable size in kb,mb or gb
     * @param String $size_in_bytes
     * @return String
     */
    static function showSize($size_in_bytes) {
        $value = 0;
        switch ($size_in_bytes) {
            case ($size_in_bytes >= Filesystem::GIG):
                $value = round($size_in_bytes/1073741824*10)/10;
                return  ($round) ? round($value) . 'Gb' : "{$value} Gb";
                break;
            case($size_in_bytes >= Filesystem::MB):
                $value = round($size_in_bytes/1048576*10)/10;
                return  ($round) ? round($value) . 'Mb' : "{$value} Mb";
                break;
            case($size_in_bytes >= Filesystem::KB):
                $value = round($size_in_bytes/1024*10)/10;
                return  ($round) ? round($value) . 'Kb' : "{$value} Kb";
                break;
            default:
                return "{$size_in_bytes} Bytes";
        }
    }
}
?>
